package org.deadline.model.config;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.deadline.model.baseapp.OrganizationDataOnDemand;
import org.deadline.model.user.PersonDataOnDemand;
import org.deadline.repository.config.UIControlConfigurationRepository;
import org.deadline.service.config.UIControlConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

@Component
@Configurable
public class UIControlConfigurationDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<UIControlConfiguration> data;

	@Autowired
    private OrganizationDataOnDemand organizationDataOnDemand;

	@Autowired
    private PersonDataOnDemand personDataOnDemand;

	@Autowired
    private RoleDataOnDemand roleDataOnDemand;

	@Autowired
    private EnumerationDataOnDemand enumerationDataOnDemand;

	@Autowired
    UIControlConfigurationService uIControlConfigurationService;

	@Autowired
    UIControlConfigurationRepository uIControlConfigurationRepository;

	public UIControlConfiguration getNewTransientUIControlConfiguration(int index) {
        UIControlConfiguration obj = new UIControlConfiguration();
        setConfigKey(obj, index);
        setConfigSection(obj, index);
        setControlType(obj, index);
        setCreationDate(obj, index);
        setDefaultconfigValues(obj, index);
        setDeleted(obj, index);
        setDescription(obj, index);
        setEnabled(obj, index);
        setModel(obj, index);
        setModificationDate(obj, index);
        setOptionconfigValuess(obj, index);
        setPermission(obj, index);
        setRole(obj, index);
        setUiLabel(obj, index);
        setUiOrder(obj, index);
        setUiconfigKey(obj, index);
        return obj;
    }

	public void setConfigKey(UIControlConfiguration obj, int index) {
        String configKey = "configKey_" + index;
        if (configKey.length() > 255) {
            configKey = configKey.substring(0, 255);
        }
        obj.setConfigKey(configKey);
    }

	public void setConfigSection(UIControlConfiguration obj, int index) {
        String configSection = "configSection_" + index;
        if (configSection.length() > 255) {
            configSection = configSection.substring(0, 255);
        }
        obj.setConfigSection(configSection);
    }

	public void setControlType(UIControlConfiguration obj, int index) {
        String controlType = "controlType_" + index;
        if (controlType.length() > 100) {
            controlType = controlType.substring(0, 100);
        }
        obj.setControlType(controlType);
    }

	public void setCreationDate(UIControlConfiguration obj, int index) {
        Date creationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setCreationDate(creationDate);
    }

	public void setDefaultconfigValues(UIControlConfiguration obj, int index) {
        String defaultconfigValues = "defaultconfigValues_" + index;
        if (defaultconfigValues.length() > 255) {
            defaultconfigValues = defaultconfigValues.substring(0, 255);
        }
        obj.setDefaultconfigValues(defaultconfigValues);
    }

	public void setDeleted(UIControlConfiguration obj, int index) {
        Boolean deleted = true;
        obj.setDeleted(deleted);
    }

	public void setDescription(UIControlConfiguration obj, int index) {
        String description = "description_" + index;
        if (description.length() > 1000) {
            description = description.substring(0, 1000);
        }
        obj.setDescription(description);
    }

	public void setEnabled(UIControlConfiguration obj, int index) {
        Boolean enabled = true;
        obj.setEnabled(enabled);
    }

	public void setModel(UIControlConfiguration obj, int index) {
        String model = "model_" + index;
        if (model.length() > 255) {
            model = model.substring(0, 255);
        }
        obj.setModel(model);
    }

	public void setModificationDate(UIControlConfiguration obj, int index) {
        Date modificationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setModificationDate(modificationDate);
    }

	public void setOptionconfigValuess(UIControlConfiguration obj, int index) {
        String optionconfigValuess = "optionconfigValuess_" + index;
        if (optionconfigValuess.length() > 1000) {
            optionconfigValuess = optionconfigValuess.substring(0, 1000);
        }
        obj.setOptionconfigValuess(optionconfigValuess);
    }

	public void setPermission(UIControlConfiguration obj, int index) {
        String permission = "permissi_" + index;
        if (permission.length() > 10) {
            permission = permission.substring(0, 10);
        }
        obj.setPermission(permission);
    }

	public void setRole(UIControlConfiguration obj, int index) {
        Role role = roleDataOnDemand.getRandomRole();
        obj.setRole(role);
    }

	public void setUiLabel(UIControlConfiguration obj, int index) {
        String uiLabel = "uiLabel_" + index;
        if (uiLabel.length() > 100) {
            uiLabel = uiLabel.substring(0, 100);
        }
        obj.setUiLabel(uiLabel);
    }

	public void setUiOrder(UIControlConfiguration obj, int index) {
        Integer uiOrder = new Integer(index);
        obj.setUiOrder(uiOrder);
    }

	public void setUiconfigKey(UIControlConfiguration obj, int index) {
        String uiconfigKey = "uiconfigKey_" + index;
        if (uiconfigKey.length() > 100) {
            uiconfigKey = uiconfigKey.substring(0, 100);
        }
        obj.setUiconfigKey(uiconfigKey);
    }

	public UIControlConfiguration getSpecificUIControlConfiguration(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        UIControlConfiguration obj = data.get(index);
        Long id = obj.getId();
        return uIControlConfigurationService.findUIControlConfiguration(id);
    }

	public UIControlConfiguration getRandomUIControlConfiguration() {
        init();
        UIControlConfiguration obj = data.get(rnd.nextInt(data.size()));
        Long id = obj.getId();
        return uIControlConfigurationService.findUIControlConfiguration(id);
    }

	public boolean modifyUIControlConfiguration(UIControlConfiguration obj) {
        return false;
    }

	public void init() {
        int from = 0;
        int to = 10;
        data = uIControlConfigurationService.findUIControlConfigurationEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'UIControlConfiguration' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<UIControlConfiguration>();
        for (int i = 0; i < 10; i++) {
            UIControlConfiguration obj = getNewTransientUIControlConfiguration(i);
            try {
                uIControlConfigurationService.saveUIControlConfiguration(obj);
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            uIControlConfigurationRepository.flush();
            data.add(obj);
        }
    }
}
